package com.luhui.ehui.util;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with LXD
 * author:Luhui
 * Date:2018-03-01
 * Time:20:48
 */
public class ConvertService {
    private static Map<Class<?>,ValueConverter> map = new HashMap<Class<?>,ValueConverter>();

    private ConvertService(){}

    public static <T> void registerConverter(ValueConverter<T> valueConverter){
        Type type = valueConverter.getClass().getGenericInterfaces()[0];
        Class<?> clazz = Object.class;
        if(type instanceof ParameterizedType){
            clazz = (Class<T>)(((ParameterizedType)type).getActualTypeArguments()[0]);
        }
        map.put(clazz,valueConverter);
    }

   public static <T> ValueConverter<T> getConverter(Class<?> clazz){
       ValueConverter<T> valueConverter =map.get(clazz);
       return valueConverter;
   }
}
